<template>
  <div>
    <div class="card" style="width: 80%; padding: 40px; background-color: bisque">
      <el-form :model="data.form" ref="formRef" label-with="100px" style="padding-right: 30px">
        <el-form-item label="头&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;像" >
          <el-upload  class="avatar-uploader" action="http://localhost:9090/files/upload" :show-file-list="false"
                      :on-success="handelImgUploadSuccess">
<!--            <el-button type="primary">上传头像</el-button>-->
            <img v-if="data.form.avatar" :src="data.form.avatar" class="avatar">
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="学生账号" prop="username" :label-width="formLabelWidth">
          <el-input v-model="data.form.username" autocomplete="off" disabled/>
        </el-form-item>
        <el-form-item label="学生密码" prop="password">
          <el-input v-model="data.form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生名称" >
          <el-input v-model="data.form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手&nbsp;机&nbsp;号" >
          <el-input v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱" >
          <el-input v-model="data.form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别" >
          <el-radio-group v-model="data.form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日" >
          <el-date-picker style="width: 100%" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="data.form.birth"></el-date-picker>
        </el-form-item>
        <el-button type="primary" @click="update">保 存</el-button>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {reactive} from "vue";
import {ElMessage} from "element-plus";
import request from "@/utils/request";
import router from "@/router";
import {Plus} from "@element-plus/icons-vue";

const data = reactive({
  form:JSON.parse(localStorage.getItem('student-user')||"{}")
})

const handelImgUploadSuccess = (res) => {
  data.form.avatar = res.data;
}

const update = () => {
  request.put('/student/update',data.form).then(res => {
    if (res.code === '200') {
      ElMessage.success("操作成功")
      router.push('/login')
      // 更新个人信息后需要重新登录
    } else {
      ElMessage.error(res.msg)
    }
  })
}

</script>

<style>
.avatar-uploader .avatar {
  width: 180px;
  height: 180px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 180px;
  height: 180px;
  text-align: center;
}
</style>